package com.ruoyi.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.DeleteObjectsResult;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;

public class AliyunOssUtil {

	private static final String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
//	private static final String endpoint = "http://oss-cn-hangzhou-internal.aliyuncs.com";
	private static final String accessKeyId = "LTAIA0SLsb0z7JWe";
	private static final String accessKeySecret = "sN8mtEef9ojq56ee4Lt4AvO1TTSFqw";
	private static final String bucketName = "osskatimes";
	private static final String style = "image/auto-orient,1/interlace,1/quality,q_30/format,jpg/watermark,text_a2F0aW1lcw,type_ZHJvaWRzYW5zZmFsbGJhY2s,color_f5f4ef,size_20,rotate_360,shadow_100,g_se,x_10,y_10";
	private static OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

	/**
	 * 上传文件到oss服务器
	 * @param path
	 * @param inputStream
	 * @return
	 */
	public static String upload(String path, InputStream inputStream) {
		String objectName =  PropertiesConst.ALIYUN_IMG_BASE + path;
		ossClient.putObject(bucketName, objectName, inputStream);
//		Date expiration = new Date(new Date().getTime() + 1000 * 60 * 10);
//		GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, objectName, HttpMethod.GET);
//		req.setExpiration(expiration);
//		req.setProcess(style);
//		URL generatePresignedUrl = ossClient.generatePresignedUrl(req);
		return access(path);
	}
	
	public static String uploadHtml(String path, InputStream inputStream) {
		String objectName =  PropertiesConst.ALIYUN_HTML_BASE + path;
		ossClient.putObject(bucketName, objectName, inputStream);
		return accesshtml(path);
	}
	
	/**
	 * 单个文件删除方法
	 * @param path  不包括域名 不包括后缀("?"以及后面)
	 * @return
	 */
	public static String deleteFile(String path) {
		  ossClient.deleteObject(bucketName, path);
		  ossClient.shutdown();
		  return "success";
		
	}
	
	/**
	 * 多个文件删除方法
	 * @param path  不包括域名 不包括后缀("?"以及后面)
	 * @return
	 */
	public static String deleteFileBatch(List<String> keys) {
		DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(keys));
		List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();//返回被删除的文件地址
		// 关闭OSSClient。
		ossClient.shutdown();
	    return "success";
		
	}
	

	/**
	 * 访问oss服务器文件
	 * @param path
	 * @return
	 */
	public static String access_private(String path) {
		
		if (!path.startsWith(PropertiesConst.PREFIX_MERCHANT_QUALIFICATION)
				&& path.indexOf(PropertiesConst.PREFIX_MERCHANT_QUALIFICATION) > 0) {
			path = path.substring(path.indexOf(PropertiesConst.PREFIX_MERCHANT_QUALIFICATION));
		}
		String objectName = PropertiesConst.ALIYUN_IMG_BASE + path;
		// 指定过期时间为10分钟。
		Date expiration = new Date(new Date().getTime() + 1000 * 60 * 10);
		GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, objectName, HttpMethod.GET);
		req.setExpiration(expiration);
		req.setProcess(style);
		URL generatePresignedUrl = ossClient.generatePresignedUrl(req);
		return generatePresignedUrl.toString();
	}
	
	public static String access(String path) {
		if (!path.startsWith(PropertiesConst.PREFIX_MERCHANT_QUALIFICATION)
				&& path.indexOf(PropertiesConst.PREFIX_MERCHANT_QUALIFICATION) > 0) {
			path = path.substring(path.indexOf(PropertiesConst.PREFIX_MERCHANT_QUALIFICATION));
		}
		//http://img.cabinh.com/dir/15072409401/C70C73C1ED214549BF22BEE22DF1C78D.20190510162150.jpg?x-oss-process=style/ksd
		String url = PropertiesConst.ALIYUN_IMG + PropertiesConst.ALIYUN_IMG_BASE + path + "?x-oss-process=style/ksd";
		return url;
	}
	
	public static String accesshtml(String path) {
		if (!path.startsWith(PropertiesConst.PREFIX_MERCHANT_QUALIFICATION)
				&& path.indexOf(PropertiesConst.PREFIX_MERCHANT_QUALIFICATION) > 0) {
			path = path.substring(path.indexOf(PropertiesConst.PREFIX_MERCHANT_QUALIFICATION));
		}
		String url = PropertiesConst.ALIYUN_IMG + PropertiesConst.ALIYUN_HTML_BASE + path;
		return url;
	}

	public static void main(String[] args) throws FileNotFoundException {
		// Endpoint以杭州为例，其它Region请按实际情况填写。

		//String objectName = "230fcaf28d18388f1950af3c5197bd59.html";

		// 创建OSSClient实例。
		ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

//		File file = new File("/Users/knight.yang/Documents/dev/wxgzh.png");
		//File file = new File("/Users/knight.yang/Documents/dev/news/230fcaf28d18388f1950af3c5197bd59.html");
//		https://img.cabinh.com/html/html/e10f88899d3107b41ced324702ba4742.html?spm=5176.8466032.0.dopenurl.24011450XSswbU
		//String upload = uploadHtml(objectName, new FileInputStream(file));
		//System.out.println(upload);
////		InputStream inputStream = new FileInputStream(file);
////		ossClient.putObject(bucketName, objectName, inputStream);
//
////		// 设置图片处理样式。
////		String style = "image/auto-orient,1/quality,q_90/watermark,text_a2F0aW1lcw,type_ZHJvaWRzYW5zZmFsbGJhY2s,color_ecead7,size_20,rotate_360,shadow_100,g_se,x_10,y_10";
//		String style = "image/auto-orient,1/interlace,1/quality,q_30/format,jpg/watermark,text_a2F0aW1lcw,type_ZHJvaWRzYW5zZmFsbGJhY2s,color_f5f4ef,size_20,rotate_360,shadow_100,g_se,x_10,y_10";
//		// 指定过期时间为10分钟。
//		Date expiration = new Date(new Date().getTime() + 1000 * 60 * 10);
//		GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, objectName, HttpMethod.GET);
//		req.setExpiration(expiration);
//		req.setProcess(style);
//		URL generatePresignedUrl = ossClient.generatePresignedUrl(req);
//		System.out.println(generatePresignedUrl);
		// 关闭OSSClient。
		//ossClient.shutdown();
	}

}
